<?php
/**
*公共函数
**/
/**
 * 返回json数据
 * @param  string       $checkMsg       错误提示
 * @param  integer      $checkCode      错误码
 * @param  undefind     $apiResponse    接口返回信息
 * @return string                       json数据
 */
function returnJson($checkMsg = '', $checkCode = 500, $apiResponse = null)
{
    return json_encode(array('code' => $checkCode, 'msg' => $checkMsg, 'info' => $apiResponse));
}
/**
 * 检测传入手机号码格式
 * @param  string $mobile 接收短信的手机号码
 * @return bool           验证结果
 */
function checkMobile($mobile)
{
    $reg = "/^1[3456789]\d{9}$/";
    return (preg_match($reg, $mobile));
}
/**
 * 检测请求ip是否在白名单
 * @param  string $ip       ip地址
 * @param  string $boc      行标示
 * @return bool             验证结果
 */
function checkIp($type, $ip, $boc)
{
    $configArr = getConfig($boc,$type);
    if (empty($configArr) || empty($configArr['ips'])) {
        return false;
    }

    $ipArr = explode(',', $configArr['ips']);
    return in_array($ip, $ipArr);
}
/*日志写入接口*/
if(!function_exists('strFormat')) 
{
  function write_log($arr=array(),$type='all')
  {
    $arr = array_merge($_REQUEST,$arr);
    /*当前时间*/
    $now = date('Y-m-d H:i:s');
    /*日志文件路径*/
    $destination = dirname(__FILE__).'/../log/'.$type.'/'.date('Ym').'/'. date('d') . '.log';
    $path = dirname($destination);
      !is_dir($path) && mkdir($path, 0755, true);
      //检测日志文件大小，超过配置大小则备份日志文件重新生成
      if (is_file($destination) && floor(2097152) <= filesize($destination)) 
      {
          rename($destination, dirname($destination) .'/'. $_SERVER['REQUEST_TIME'] . '-' . basename($destination));
      }
      $arr['timestamp'] = $_SERVER['REQUEST_TIME'];
      $arr['server_addr'] = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '0.0.0.0';
      $arr['remote_addr'] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
      $arr['method'] = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'CLI';
      $arr['url'] = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
      $str = var_export($arr,true);
      error_log('['.$now."]\r\n".$str."\r\n---------------------------------------------------------------\r\n",3,$destination);
  }
}